package cz.acies.face.viewers;

import java.lang.reflect.Type;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;


public class TableColumnDefine extends ShuttleListItemDefine implements JsonSerializer<TableColumnDefine>
{
	public final static int TEXT = 0;
	public final static int NUMBER = 1;
	public final static int DECIMAL = 2;
	public final static int MONEY = 3;
	public final static int DATE = 4;

	private static final long serialVersionUID = 4597289345505238387L;
	
	protected String field;			
	protected int width;
	protected String alignment;
	protected String type;
	protected String pattern;
	protected boolean sortable;
	protected boolean editable;
	protected boolean resizable;
	protected boolean hidden;		
	
	//String field;			
	//int width;
	//title
	//rowspan		number
	//colspan		number
	//align			string
	//halign		string
	//sortable		boolean
	//order			string
	//resizable		boolean
	//fixed			boolean
	//hidden		boolean
	//checkbox		boolean
	//formatter
	
	public TableColumnDefine() {}

	public TableColumnDefine(String caption, boolean sortable) {
		this.caption = caption;
		this.sortable = sortable;
	}

	public TableColumnDefine(String id, String caption, int width, String alignment, String type, String pattern) {
		this(id, caption, width, alignment, type, pattern, false, new Integer(0));
	}
	
	public TableColumnDefine(String id, String caption, int width, String alignment, String type, String pattern, boolean sortable, Integer priority) {
		super();
		this.id = id;
		this.caption = caption;
		this.width = width;
		this.pattern = pattern;
		this.sortable = sortable;
		this.priority = priority;
		
		setType(type);
		setAlignment(alignment);
	}

	// -----  -----------------------------------------------------------------
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public String getAlignment() {
		return alignment;
	}

	public void setAlignment(String alignment) {
		if (alignment == null) {
			alignment = "left";
		} else {
			alignment = alignment.toLowerCase();
			if (!alignment.equals("left") || !alignment.equals("center") || alignment.equals("right")) {
				alignment = "left";
			}
		}
		this.alignment = alignment;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		if (type == null) {
			type ="string";
		} else {
			type = type.toLowerCase();
			if (type.equals("")) { 
				type ="string";	
			}
		}
		this.type = type;
	}
	
	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public boolean isSortable() {
		return sortable;
	}

	public void setSortable(boolean isSorted) {
		this.sortable = isSorted;
	}

	public boolean isHidden() {
		return hidden;
	}

	public TableColumnDefine setHidden(boolean hidden) {
		this.hidden = hidden;
		return this;
	}

	// -----  -----------------------------------------------------------------
	/*
	public static TableColumnDefine buildInstance(Element node) {
		if (node != null && node.getName().equals("COLUMN_DEFINE")) {
			String id = node.attributeValue("id");
			String caption = node.attributeValue("caption");
			int width = Integer.valueOf(node.attributeValue("width"));
			int alignment = Integer.valueOf(node.attributeValue("alignment"));
			String pattern = node.attributeValue("pattern");
			return new TableColumnDefine(id, caption, width, alignment, pattern, true, -1);
		}
		return null;
	}
	
	public static TableColumnDefine buildInstance(String xml) {
		try {
			Document doc = DocumentHelper.parseText(xml);
			Element node = doc.getRootElement();
			return buildInstance(node);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return null;
	}
	*/

	@Override
	public JsonElement serialize(TableColumnDefine define, Type type, JsonSerializationContext context) {
		final JsonObject json = new JsonObject();

		json.addProperty("field", define.getId());
		json.addProperty("title", define.getCaption());
		json.addProperty("width", define.getWidth()); 
		//json.addProperty("dataType", define.getType());
		//json.addProperty("align", define.getAlignment());
		json.addProperty("hidden", define.isHidden());
		//json.addProperty("editable", false);
		//json.addProperty("sortable", define.isSortable());
		
		//title
		//field			
		//width
		//rowspan		number
		//colspan		number
		//align			string
		//halign		string
		//sortable		boolean
		//order			string
		//resizable		boolean
		//fixed			boolean
		//hidden		boolean
		//checkbox		boolean
		//formatter

	    return json;
	}

}
